<?php
// 游戏初始化配置
$boardSize = 8;
$initialFruits = [
    '1' => '樱桃',
    '2' => '草莓',
    '3' => '橘子',
    '4' => '柠檬',
    '5' => '猕猴桃',
    '6' => '西红柿',
    '7' => '菠萝',
    '8' => '椰子',
    '9' => '西瓜'
];
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>合成大西瓜</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
        }

        #game-board {
            display: grid;
            grid-template-columns: repeat(<?php echo $boardSize; ?>, 50px);
            grid-template-rows: repeat(<?php echo $boardSize; ?>, 50px);
            gap: 5px;
            justify-content: center;
            margin-top: 20px;
        }

        .cell {
            border: 1px solid #ccc;
            background-color: #f0f0f0;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    </style>
</head>

<body>
    <h1>合成大西瓜</h1>
    <div id="game-board">
        <?php
        for ($i = 0; $i < $boardSize; $i++) {
            for ($j = 0; $j < $boardSize; $j++) {
                echo '<div class="cell" data-row="' . $i . '" data-col="' . $j . '"></div>';
            }
        }
        ?>
    </div>

    <script>
        const board = document.getElementById('game-board');
        const cells = board.querySelectorAll('.cell');
        const fruits = <?php echo json_encode($initialFruits); ?>;

        // 随机生成一个水果到棋盘上
        function generateRandomFruit() {
            const emptyCells = Array.from(cells).filter(cell => cell.textContent === '');
            if (emptyCells.length > 0) {
                const randomCell = emptyCells[Math.floor(Math.random() * emptyCells.length)];
                const randomFruitLevel = 1; // 初始水果等级为 1
                randomCell.textContent = fruits[randomFruitLevel];
                randomCell.dataset.fruitLevel = randomFruitLevel;
            }
        }

        // 检查相邻相同水果并合并
        function checkAndMerge() {
            for (let i = 0; i < <?php echo $boardSize; ?>; i++) {
                for (let j = 0; j < <?php echo $boardSize; ?>; j++) {
                    const currentCell = board.querySelector(`.cell[data-row="${i}"][data-col="${j}"]`);
                    if (currentCell.dataset.fruitLevel) {
                        const currentLevel = parseInt(currentCell.dataset.fruitLevel);

                        // 检查右侧相邻
                        if (j < <?php echo $boardSize; ?> - 1) {
                            const rightCell = board.querySelector(`.cell[data-row="${i}"][data-col="${j + 1}"]`);
                            if (rightCell.dataset.fruitLevel && parseInt(rightCell.dataset.fruitLevel) === currentLevel) {
                                const newLevel = currentLevel + 1;
                                if (fruits[newLevel]) {
                                    currentCell.textContent = fruits[newLevel];
                                    currentCell.dataset.fruitLevel = newLevel;
                                    rightCell.textContent = '';
                                    delete rightCell.dataset.fruitLevel;
                                }
                            }
                        }

                        // 检查下方相邻
                        if (i < <?php echo $boardSize; ?> - 1) {
                            const bottomCell = board.querySelector(`.cell[data-row="${i + 1}"][data-col="${j}"]`);
                            if (bottomCell.dataset.fruitLevel && parseInt(bottomCell.dataset.fruitLevel) === currentLevel) {
                                const newLevel = currentLevel + 1;
                                if (fruits[newLevel]) {
                                    currentCell.textContent = fruits[newLevel];
                                    currentCell.dataset.fruitLevel = newLevel;
                                    bottomCell.textContent = '';
                                    delete bottomCell.dataset.fruitLevel;
                                }
                            }
                        }
                    }
                }
            }
        }

        // 初始化游戏
        function initGame() {
            generateRandomFruit();
        }

        // 每 3 秒生成一个新水果
        setInterval(() => {
            generateRandomFruit();
            checkAndMerge();
        }, 3000);

        initGame();
    </script>
</body>

</html>